package product;

import java.util.HashSet;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class ProviceType {

	public static void main(String[] args) throws Exception{
		if (args.length!=2) {
			System.err.println("path err!");
			System.exit(-1);
		}
		Job job=new Job(new Configuration(),"prouductype");
		job.setJarByClass(ProviceType.class);
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		job.setMapperClass(ptMaps.class);
		job.setReducerClass(ptReduces.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		job.waitForCompletion(true);
	}
	public static class ptMaps extends Mapper<LongWritable, Text,Text,Text>{
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			String[] lines = value.toString().split("\t");
			if (lines.length==6) {
				String name=lines[0].trim();
				String provice=lines[4].trim();
				context.write(new Text(provice),new Text(name));
			}
		};
	}
	public static class ptReduces extends Reducer<Text,Text,Text,IntWritable>{
		protected void reduce(Text k2, java.lang.Iterable<Text> values, org.apache.hadoop.mapreduce.Reducer<Text,Text,Text,IntWritable>.Context context) throws java.io.IOException ,InterruptedException {
			HashSet<String> sets=new HashSet<String>();
			for (Text value:values) {
				sets.add(value.toString());
			}
			context.write(k2, new IntWritable(sets.size()));
		};
	}

}
